namespace Vanilla.Themes.Eclipse

import Vanilla.UI
import Vanilla

class EclipseTheme (DefaultTheme):
	override def DefaultForegroundColourFor (g as Gadget) as Colour:
		if g.Parent isa MenuItem:
			return Colour(0,0,0)
		else:
			return Colour(0.9,1.0,0.9)
		
	class WindowFadeOutProperty (ThemedProperties):
		public Alpha as double
		def constructor (a as double):
			Alpha = a
	
	class WindowAlreadyFadedProperty (ThemedProperties):
		pass
	
	override def DrawWindow (g as Window, b as Brush):
		if g.ContainsMouse:
			g.ThemedProperties = null
		elif g.ThemedProperties is null:
			g.ThemedProperties = WindowFadeOutProperty(1.0)
			UITimer(100) def (t as UITimer):
				if (not g.ThemedProperties isa WindowFadeOutProperty) or (g.ThemedProperties as WindowFadeOutProperty).Alpha <= 0.75:
					t.Stop()
					return
				p = g.ThemedProperties as WindowFadeOutProperty
				p.Alpha -= 0.025
				g.Redraw()
	
		unselected_opacity = 0.75
		if g.ThemedProperties isa WindowFadeOutProperty:
			unselected_opacity = (g.ThemedProperties as WindowFadeOutProperty).Alpha
		
		// Construct the rounded outline
		b.DrawRoundedRect(Rect(g.Size), 10)
		
		// Fill the window
		if g.ContainsMouse:
			gr = LinearGradient(0,0,0,g.Size.Height)
			gr.Add(0,Colour.Grey)
			gr.Add(g.BottomOfTop/g.Size.Height, Colour.DarkGrey)
			gr.Add((g.BottomOfTop+g.Spacing)/g.Size.Height, Colour.Black)
			gr.Add(1,Colour.DarkGrey)
			b.Pattern = gr
		else:
			b.Colour = Colour(0,0,0,unselected_opacity)
		b.FillPreserve()
		
		// Draw the outline
		b.LineWidth = 2
		if g.ContainsMouse:
			b.Colour = Colour.White
		else:
			b.Colour = Colour.Grey
		b.Stroke()
		
		// Draw the seperator, but only if it looks like it's needed
		unless g.Child isa Box and (g.Child as Box).DrawFrame:
			b.LineWidth = 1
			gr = LinearGradient(0,0,g.Size.Width,0)
			gr.Add(0, Colour(0,0,0,0))
			gr.Add(0.25, Colour(0.8,0.8,0.8,0.5))
			gr.Add(0.75, Colour(0.8,0.8,0.8,0.5))
			//gr.Add(15/g.Size.Width, Colour(0.8,0.8,0.8,0.5))
			//gr.Add((g.Size.Width - 15)/g.Size.Width, Colour(0.8,0.8,0.8,0.5))
			gr.Add(1, Colour(0,0,0,0))
			
			b.Pattern = gr
			//b.Colour = Colour.LightGrey.Alpha(0.8)
			b.MoveTo(5, g.BottomOfTop + g.Spacing/2)
			b.LineTo(g.Size.Width-5, g.BottomOfTop + g.Spacing/2)
			b.Stroke()
	
	def ButtonGradient (r as Rect, c as Colour):
		gr = LinearGradient(0,r.Y,0,r.Y2)
		gr.Add(0, c * 1.05)
		gr.Add(0.48, c)
		gr.Add(0.52, c * 0.5)
		gr.Add(1, c)
		return gr
		
	override def MouseOverChangesLookFor (g as Gadget) as bool:
		if g isa ButtonBar:
			return true
		else:
			return super(g)
	
	override def DrawButton (g as Button, b as Brush):
		b.LineWidth = 2
		
		if g.Parent isa ButtonBar:
			b.Draw(Rect(g.Size))
		else:
			b.DrawRoundedRect(Rect(1, 1, g.Rect.Width - 1, g.Rect.Height - 1), 10)
		
		if g.ContainsMouse:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Grey * 1.2)
		else:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Grey)
		
		b.FillPreserve()
		
		if g.Parent isa ButtonBar:
			b.LineWidth = 2
			gr = LinearGradient(0,0,0,g.Size.Height)
			gr.Add(0,Colour.LightGrey.Alpha(0))
			if g.ContainsMouse:
				gr.Add(0.25, Colour.White)
				gr.Add(0.75, Colour.White)
			else:
				gr.Add(0.25, Colour.LightGrey)
				gr.Add(0.75, Colour.LightGrey)
			gr.Add(1,Colour.LightGrey.Alpha(0))
			b.Pattern = gr
		else:
			if g.ContainsMouse:
				b.Colour = Colour.White
			else:
				b.Colour = Colour.LightGrey
				
		b.Stroke()
	
	override def DrawMenuItem (g as MenuItem, b as Brush):
		return unless g.ContainsMouse
		
		b.LineWidth = 2
		b.Draw(Rect(2,0,g.Size.Width-2,g.Size.Height))
		
		b.Pattern = ButtonGradient(Rect(g.Size), Colour(0.5,0.5,0.5,0.75))
		b.FillPreserve()
		
		b.Colour = Colour.Grey
		b.Stroke()
	
	override def DrawBox (g as Box, b as Brush):
		return unless g.DrawFrame
	
		b.LineWidth = 1
		
		b.DrawRoundedRect(Rect(1,1,g.Size.Width-1,g.Size.Height-1), 10)
		
		b.Colour = Colour(0.3,0.3,0.3,0.5)
		b.FillPreserve()
		
		b.Colour = Colour(0.65,0.65,0.65)
		b.Stroke()
	
	override def DrawWindowCloseButton (g as Window.CloseButton, b as Brush):
		b.LineWidth = 1
		
		b.DrawRoundedRect(Rect(1, 1, g.Rect.Width - 1, g.Rect.Height - 1), 10)
		
		if g.ContainsMouse:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Red)
		else:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Red.Alpha(0.25))
		
		b.FillPreserve()
		
		if g.ContainsMouse:
			b.Colour = Colour.White
		else:
			b.Colour = Colour.LightGrey
		b.Stroke()
	
	override def DrawVScrollerGrabby (g as VScroller.Grabby, b as Brush):
		b.LineWidth = 1
		b.DrawRoundedRect(Rect(1,1,g.Size.Width,g.Size.Height), 10)
		
		gr = LinearGradient(0,0,g.Size.Width,0)
		if g.ContainsMouse:
			gr.Add(0, Colour.Grey)
			gr.Add(0.5, Colour.Grey * 1.1)
			gr.Add(1, Colour.Grey)
		else:
			gr.Add(0, Colour.Grey * 0.9)
			gr.Add(0.5, Colour.Grey)
			gr.Add(1, Colour.Grey * 0.9)
		b.Pattern = gr
		b.FillPreserve()
		
		if g.ContainsMouse:
			b.Colour = Colour.White
		else:
			b.Colour = Colour.LightGrey
		b.Stroke()
	
	override def DrawPanel (g as Panel, b as Brush):
		b.LineWidth = 2
		b.Draw(Rect(g.Size))
		
		b.Pattern = ButtonGradient(Rect(g.Size), Colour(0.2,0.2,0.2,0.95))
		b.FillPreserve()
		
		gr = LinearGradient(0,0,0,g.Size.Height)
		gr.Add(0, Colour.White)
		gr.Add(1, Colour.White.Alpha(0))
		
		b.Pattern = gr
		b.Stroke()
	
	override def DrawWindowMenuButton (g as Window.MenuButton, b as Brush):
		b.LineWidth = 1
		
		b.DrawRoundedRect(Rect(1, 1, g.Rect.Width - 1, g.Rect.Height - 1), 10)
		
		if g.ContainsMouse:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Green)
		else:
			b.Pattern = ButtonGradient(Rect(g.Size), Colour.Green.Alpha(0.25))
		
		b.FillPreserve()
		
		if g.ContainsMouse:
			b.Colour = Colour.White
		else:
			b.Colour = Colour.LightGrey
		b.Stroke()
	
	override def DrawButtonBar (g as ButtonBar, b as Brush):
		b.DrawRoundedRect(Rect(1,1,g.Size.Width-1,g.Size.Height-1), 10)
		b.LineWidth = 2
		if g.ContainsMouse:
			b.Colour = Colour.White
		else:
			b.Colour = Colour.LightGrey
		b.Stroke()
		b.DrawRoundedRect(Rect(1.5,1.5,g.Size.Width-1.5,g.Size.Height-1.5), 10)
		b.Clip()

class EclipseThemeTool (UI.Tool, IOpen):
	def Open ():
		UI.Tool.CurrentDesktop.Theme = EclipseTheme()
